package com.action;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.beans.Manufacturer;
import com.beans.Product;
import com.hibernate.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private Product product;
	private Manufacturer provider;
	
	
	public String execute() {		
		String ManuId= ServletActionContext.getRequest().getParameter("manuid");
		ServletActionContext.getRequest().getSession().setAttribute("manuid", ManuId);
		return SUCCESS;
	}
	
	public String editPrepare(){
		String ProductId = ServletActionContext.getRequest().getParameter("SelectedProductId");
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		//this is for the case where the Edit Product function is used in the search-result page, begin:
		String returntosearch=ServletActionContext.getRequest().getParameter("returntosearch");
		if(returntosearch!=null){
			ServletActionContext.getRequest().setAttribute("returntosearch", "true");
		}
		//end
		
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			product = (Product) session.get(Product.class, Integer.parseInt(ProductId));
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		return SUCCESS;
	}
	
	public String deleteProductList() {
		String[] ProductIDList = ServletActionContext.getRequest().getParameterValues("IdCheck");
		
		//these are for the action returnproduct
		String ManuId = ServletActionContext.getRequest().getParameter("manuid");
		ServletActionContext.getRequest().getSession().setAttribute("SelectedManuId", ManuId);
		
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		transaction = session.beginTransaction();
		try {
			if(ProductIDList != null) {
				for(int i = 0; i < ProductIDList.length; i++) {
					product = (Product) session.get(Product.class, Integer.parseInt(ProductIDList[i]));
					session.delete(product);	
				}
			
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}
		finally {
			session.close();
		}
		return SUCCESS;
	}
	
	public String addProduct() {
		String ManuId= ServletActionContext.getRequest().getParameter("manufacturerID");
		
		//this is for action returnproduct
		 ServletActionContext.getRequest().getSession().setAttribute("SelectedManuId", ManuId);
		
		
		String price = ServletActionContext.getRequest().getParameter("price");
		String quantity = ServletActionContext.getRequest().getParameter("quantity");
		String des = ServletActionContext.getRequest().getParameter("des");
		String name = ServletActionContext.getRequest().getParameter("name");
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
		
			provider=(Manufacturer) session.get(Manufacturer.class, Integer.parseInt(ManuId));
			product = new Product();
			product.setName(name);
			product.setPrice(Long.parseLong(price));
			product.setQuantity(Integer.parseInt(quantity));
			product.setDes(des);
			product.setManufacturer(provider);
		
			session.save(product);
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		return SUCCESS;
	}
	
	public String editProduct() {
		
		String ProductId = ServletActionContext.getRequest().getParameter("productid");
		String price = ServletActionContext.getRequest().getParameter("price");
		String quantity = ServletActionContext.getRequest().getParameter("quantity");
		String des = ServletActionContext.getRequest().getParameter("des");
		String name = ServletActionContext.getRequest().getParameter("name");
		String ManuId = ServletActionContext.getRequest().getParameter("manufacturerid");
		
		//this is also for the action returnproduct
		ServletActionContext.getRequest().getSession().setAttribute("SelectedManuId", ManuId);
		
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
		
			provider = (Manufacturer) session.get(Manufacturer.class, Integer.parseInt(ManuId));
			product = (Product) session.get(Product.class, Integer.parseInt(ProductId));
			product.setDes(des);
			product.setManufacturer(provider);
			product.setName(name);
			product.setPrice(Long.parseLong(price));
			product.setQuantity(Integer.parseInt(quantity));
		
			session.update(product);
			transaction.commit();}
		catch(HibernateException e){
			transaction.rollback();
			e.printStackTrace();}
		finally{
			session.close();}
		return SUCCESS;
	}

	
	public Product getProduct(){
		return product ;
	}
	
	public void setProduct(Product product){
		this.product = product;
	}

	public void setProvider(Manufacturer provider) {
		this.provider = provider;
	}

	public Manufacturer getProvider() {
		return provider;
	}
	
}
